package com.kgc.warehouse.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.dto.DeptPageDto;
import com.kgc.warehouse.system.entity.Dept;
import com.kgc.warehouse.system.entity.User;
import com.kgc.warehouse.system.mapper.DeptMapper;
import com.kgc.warehouse.system.mapper.UserMapper;
import com.kgc.warehouse.system.service.DeptService;
import com.kgc.warehouse.system.vo.DeptVo;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author main
 * @description 针对表【sys_dept】的数据库操作Service实现
 * @createDate 2023-02-03 20:07:43
 */
@Service
@Slf4j
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
        implements DeptService {

    @Autowired(required = false)
    private DeptMapper deptMapper;
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public ResponseResult selectPage(DeptPageDto deptPageDto) {

        Page<Dept> deptPage = new Page<>(deptPageDto.getPage(), deptPageDto.getLimit());

        log.info(deptPageDto.getName());
        LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(deptPageDto.getName()), Dept::getName, deptPageDto.getName());
        deptMapper.selectPage(deptPage, deptLambdaQueryWrapper);
        long total = deptPage.getTotal();
        List<Dept> records = deptPage.getRecords();
        List<DeptVo> collect = records.stream().map(record -> {
            DeptVo deptVo = new DeptVo();
            BeanUtils.copyProperties(record, deptVo);
            Integer parentId = record.getParentId();
            if (parentId != 0) {
                Dept dept = deptMapper.selectById(parentId);
                String deptName = dept.getName();
                deptVo.setParentname(deptName);
            }

            return deptVo;
        }).collect(Collectors.toList());
        return new ResponseResult(collect, total, CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult add(DeptVo deptVo) {

        Dept dept = new Dept();
        BeanUtils.copyProperties(deptVo, dept);
        log.info(String.valueOf(dept.getParentId()));
        //先检查父级部门下有无此部门
        LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.eq(Dept::getName, deptVo.getName())
                .eq(Dept::getParentId, deptVo.getDept());
        List<Dept> depts = deptMapper.selectList(deptLambdaQueryWrapper);

        if (depts.size() > 0) {
            return new ResponseResult(CodeMsg.DEPT_EXIT);
        }
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.format(day);
        dept.setCreatetime(day);
        dept.setParentId(deptVo.getDept());
        deptMapper.insert(dept);

        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        //先判断此部门底下是否有子部门

        LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.in(Dept::getParentId, id);
        List<Dept> depts = deptMapper.selectList(deptLambdaQueryWrapper);
        if (depts.size() > 0) {
            return new ResponseResult(CodeMsg.CHILD_DEPT_EXIT);

        }


        //判断此部门下是否有员工

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.in(User::getDeptid, id);
        List<User> users = userMapper.selectList(userLambdaQueryWrapper);
        if (users.size() > 0) {
            return new ResponseResult(CodeMsg.DEPT_HASUSER);
        }

        deptMapper.deleteById(id);
        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult updateDeptById(Dept dept) {

        //先检查此父级部门下有无此次级部门
        LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.eq(Dept::getParentId,dept.getParentId())
                .eq(Dept::getName,dept.getName());

        log.info("====================================");
        Dept dept1 = deptMapper.selectOne(deptLambdaQueryWrapper);
        log.info("==============================");
        log.info(String.valueOf(dept1));
        if (ObjectUtils.isNotEmpty(dept1)&& !dept1.getId().equals(dept.getId()))
        {
            return new ResponseResult(CodeMsg.DEPT_EXIT);
        }
        deptMapper.updateById(dept);
        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




